//
//  BGMessageCountDown.m
//  bigMall
//
//  Created by LTOVE on 16/7/25.
//  Copyright © 2016年 thinklev. All rights reserved.
//  高度 分割线距离上个控件 + 距离title Magin + tit_heigh + Magin + timeView + Magin

#import "BGMessageCountDown.h"
#import "LTOVESecToDays.h"

@interface BGMessageCountDown (){
    
    NSUInteger _time;
    NSTimer *_timer;
    UILabel *_label;
    CGFloat _titleMaxY;
}

@end
@implementation BGMessageCountDown


//+ (UIView *)initWithFram:(CGRect)frame andTimesString:(NSString *)timeStr andTitle:(NSString *)title andColor:(UIColor *)color
//{
//    UIColor *c ;
//}

+ (BGMessageCountDown *)initWithFram:(CGRect)frame andTimesString:(NSString *)timeStr andTitle:(NSString *)title andColor:(UIColor *)color
{
//    GWLog(@"%@",timeStr);
    
    NSTimeInterval time = [timeStr doubleValue];
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSTimeInterval leng = [date timeIntervalSinceNow];
    if (leng < 0) {
        leng = 0;
    }
    NSUInteger sec = round(leng);
    
    GWLog(@"%f",leng);
    GWLog(@"%lu",(unsigned long)leng);
    BGMessageCountDown *view = [[BGMessageCountDown alloc]initWithFrame:frame];
    view.backgroundColor = [UIColor whiteColor];
    //分割线
    UILabel *lineLabel = [UILabel new];
    lineLabel.backgroundColor = BGtextGraykColor;
    lineLabel.height = 1;//分割线粗细
    lineLabel.width = view.width - 2 * BGMaginLeft;
    lineLabel.center = CGPointMake(view.width * 0.5, 12);
    [view addSubview:lineLabel];
    
    //标题长度
    NSUInteger lenth = title.length;
    //标题
    title = [NSString stringWithFormat:@"————    %@    ————",title];
    NSDictionary *tDic = @{
                           NSFontAttributeName:[UIFont systemFontOfSize:12],
                           //                          NSParagraphStyleAttributeName:paraStyle
                           NSForegroundColorAttributeName:[UIColor grayColor]
                           };
    NSMutableAttributedString *titAttr = [[NSMutableAttributedString alloc]initWithString:title attributes:tDic];
    //设置标题颜色
    [titAttr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(8, lenth)];
    UILabel *titleLabel = [UILabel new];
    titleLabel.attributedText = titAttr;
    [titleLabel sizeToFit];
    //    titleLabel.center = CGPointMake(view.width * 0.5, view.height * 0.25);
    titleLabel.centerY = CGRectGetMaxY(lineLabel.frame) + BGMaginTop;
    titleLabel.centerX = view.width * 0.5;
//    view
    [view addSubview:titleLabel];
    
    //创建label
    UILabel *label = [[UILabel alloc]init];
//    [view setLabelTextWithTimeStr:sec] andLabel:label];
    
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor clearColor];
//    [label sizeToFit];
//    label.centerX = view.width * 0.5;
//    label.y = CGRectGetMaxY(titleLabel.frame) + BGMaginTop;
//    label.centerY = view.height * 0.5;
    label.height = BGLabelHeigh;
    [view addSubview:label];
    [view setLabelTextWithTimeStr:sec andLabel:label andmaxTitleY:CGRectGetMaxY(titleLabel.frame)];
    
   
//    [self.view addSubview:titleLabel];
//    [self addSubview:_label];
    
    
    //创建时钟视图
//    UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"clock"] highlightedImage:[UIImage imageNamed:@"clock"]];
//    imgView.center = CGPointMake(label.x - 10, view.height * 0.5);
//    imgView.width = imgView.height = imgView.height * 0.8;
//    imgView.backgroundColor = [UIColor clearColor];
//    [view addSubview:imgView];
//    [view setLabelTextWithString:@"ds" viw:view];
    return view;
}
/**
 *  设置倒计时label的文字内容
 *
 *  @param timeStr timeStr description
 */
- (void)setLabelTextWithTimeStr:(NSUInteger )timeStr andLabel:(UILabel *)label andmaxTitleY:(CGFloat)heigh
{
    _label = label;
    _time = timeStr;
    _titleMaxY = heigh;
    
//     _time = [timeStr integerValue];
//    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
//        
////        _timer = [NSTimer scheduledTimerWithTimeInterval:_time target:self selector:@selector(starts) userInfo:nil repeats:NO];
//      
//    }];
//    [queue addOperation:op];
    [self starts];
    if (_time > 0) {
        [self setIsDelloc:YES];
        [_timer invalidate];
        _timer = nil;
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(starts) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
    }
    
}

- (void)starts
{
    NSString *timeStr = [LTOVESecToDays initWithSec:_time];
    
    if (_time> 0) {
        _time --;
    }
    
    [self setLabelTextWithString:timeStr viw:self];
//    GWLog(@"%@",timeStr);
    
//    [self setLabelTextWithString:timeStr];
}

- (void)setLabelTextWithString:(NSString *)strs viw:(UIView *)view
{
//    NSString *str = @"0 2天0 2时0 2分0 2秒";
    NSString *str = strs;
//    NSMutableParagraphStyle *paraStyle = [NSMutableParagraphStyle new];
//    paraStyle.lineHeightMultiple = 13;
//    paraStyle.minimumLineHeight = 13;
//    paraStyle.alignment = UITextAlignmentCenter;
//    
//    paraStyle.maximumLineHeight = 15;
    NSDictionary *dic = @{
                          NSFontAttributeName:[UIFont systemFontOfSize:15],
                          //                          NSParagraphStyleAttributeName:paraStyle
                          NSForegroundColorAttributeName:[UIColor grayColor]
                          };

    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:str attributes:dic];
    
    NSDictionary *blackDic = @{
                               NSForegroundColorAttributeName:[UIColor whiteColor],
                               NSBackgroundColorAttributeName:[UIColor blackColor],
                               NSFontAttributeName:[UIFont systemFontOfSize:15],
                               };
    NSDictionary *redkDic = @{
                              NSForegroundColorAttributeName:[UIColor whiteColor],
                              NSBackgroundColorAttributeName:[UIColor redColor],
                              NSFontAttributeName:[UIFont systemFontOfSize:15],
                              };
    //天设置格式
    [attString addAttributes:blackDic range:NSMakeRange(0, 1)];
    [attString addAttributes:blackDic range:NSMakeRange(2, 1)];
    //设置小时格式
    [attString addAttributes:blackDic range:NSMakeRange(4, 1)];
    [attString addAttributes:blackDic range:NSMakeRange(6, 1)];
    //分钟设置
    [attString addAttributes:blackDic range:NSMakeRange(8, 1)];
    [attString addAttributes:redkDic range:NSMakeRange(10, 1)];
    //秒设置
    [attString addAttributes:redkDic range:NSMakeRange(12, 1)];
    [attString addAttributes:redkDic range:NSMakeRange(14, 1)];
    
    [attString addAttributes:redkDic range:NSMakeRange(16, 1)];
    [attString addAttributes:redkDic range:NSMakeRange(18, 1)];
//    UILabel *label = [UILabel new];
//    _label = label;
    _label.attributedText = attString;
    
    [_label sizeToFit];
//    label.center = self.view.center;
//    _label.center = CGPointMake(view.width * 0.5, _titleMaxY + 30);
    _label.centerX = view.width * 0.5;
    _label.y = 10 + _titleMaxY;
    _label.height = BGLabelHeigh;
    _label.contentMode = UIControlContentHorizontalAlignmentCenter;
    
    
}



- (void)dealloc
{
    [_timer invalidate];
    _timer = nil;
}




//- (NSString *)secToDateStringWithSec
//{
//    NSUInteger sec = _time;
//    _time --;
////    GWLog(@"%lu",sec);
//    //获取一段时间后的日期
//    NSDate *tomoDate = [NSDate dateWithTimeIntervalSinceNow:sec];
//    
//    NSCalendar *calender = [NSCalendar currentCalendar];
//    int unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute | NSCalendarUnitSecond;
//    NSDateComponents *comTomo = [calender components:unit fromDate:tomoDate];
//    NSDateComponents *comNow = [calender components:unit fromDate:[NSDate date]];
//    
//    NSString *BasicStr = @"距离结束:";
//    NSUInteger year = comTomo.year - comNow.year;
//    NSUInteger month = comTomo.month - comNow.month;
//    NSUInteger day = comTomo.day - comNow.day;
//    NSUInteger hour = comTomo.hour - comNow.hour;
//    NSUInteger minute = comTomo.minute - comNow.minute;
//    NSUInteger seco = comTomo.second - comNow.second;
//    if (year > 0) {
//        return [NSString stringWithFormat:@"%lu年%lu月%lu天%lu时%lu分%lu秒",(unsigned long)year,(unsigned long)month,(unsigned long)day,(unsigned long)hour,(unsigned long)minute,(unsigned long)seco];
//    }else if (month > 0){
//        return [NSString stringWithFormat:@"%lu月%lu天%lu时%lu分%lu秒",(unsigned long)month,(unsigned long)day,(unsigned long)hour,(unsigned long)minute,(unsigned long)seco];
//    }else{
//        return [NSString stringWithFormat:@"%u %u天%u %u时%u %u分%u %u秒",day/10,day%10,hour/10,hour%10,minute/10,minute%10,seco/10,seco%10];
//    }
////    else if (hour > 0){
////        return [NSString stringWithFormat:@"%d时%d分%d秒",hour,minute,seco];
////    }else if (minute > 0){
////        return [NSString stringWithFormat:@"%d分%d秒",minute,seco];
////    }else if (seco > 0){
////        return [NSString stringWithFormat:@"%d秒",seco];
////    }else
////        return @"已经结束";
//}

- (void)setIsDelloc:(BOOL )isDelloc
{
//    NSLog(@"%d1111111111111111111111111111111111111111111111111",isDelloc);
//    if (isDelloc) {
//        
//    }
    [_timer invalidate];
    _timer = nil;
    [_timer invalidate];
    _timer = nil;
}

@end
